package com.lxq.LeetCode.convert;

public class Solution {
    public static void main(String[] args) {
        String res = convert("112121aascA", 1);
        System.out.println(res);
    }

    public static String convert(String s, int numRows) {
        if (numRows==1){
            return s;
        }
        StringBuilder res = new StringBuilder();
        int partLength = 2 * numRows - 2;
        int partNum = s.length() % partLength == 0 ? s.length() / partLength : s.length() / partLength + 1;
        for (int j = 0; j < partNum; j++) {
            res.append(s.charAt(j * partLength));
        }
        for (int i = 2; i < numRows; i++) {
            for (int j = 0; j < partNum; j++) {
                int temp = ((partLength - 1) / 2 + 1 - (numRows - i)) + j * partLength;
                if (temp < s.length()) {
                    res.append(s.charAt(temp));
                }
                temp = ((partLength - 1) / 2 + 1 + (numRows - i)) + j * partLength;
                if (temp < s.length()) {
                    res.append(s.charAt(temp));
                }
            }
        }
        for (int j = 0; j < partNum; j++) {
            if (((partLength - 1) / 2 + 1) + j * partLength < s.length()) {
                res.append(s.charAt(((partLength - 1) / 2 + 1) + j * partLength));
            }

        }

        return res.toString();
    }
}
